package com.wo56.pushmsgcenter.common.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.framework.core.SysContexts;
import com.framework.core.cache.vo.SysStaticData;
import com.framework.core.util.SysStaticDataUtil;

public class CheckUtil {
	private static final Log log = LogFactory.getLog(CheckUtil.class);

	public static Map checkNull(Class clazz, Map properties,String excludes) throws IllegalAccessException, InvocationTargetException{
		if(properties==null){
			return null;
		}
		String[] propertyNames= SysContexts.getClassMetadata(clazz).getPropertyNames();
		boolean[] abilitys= SysContexts.getClassMetadata(clazz).getPropertyNullability();
		Map resultMap = new HashMap();
		String propertyName="";
		for(int i=0;i<propertyNames.length;i++){
			if(!abilitys[i]&&(properties.get(propertyNames[i])==null||"".equals(properties.get(propertyNames[i])))
					&&excludes.indexOf(propertyNames[i])<0){
				propertyName+=propertyNames[i]+",";
			}
		}
		if("".equals(propertyName)){
			return null;
		}
		propertyName=propertyName.substring(0, propertyName.length()-1);
//		resultMap.put("isOk", "N");
		resultMap.put("msg", propertyName+"不能为空");
		log.error(propertyName+"不能为空");
		return resultMap;
	}
	
	public static Map checkCode(Map properties,String propertyName,String codeType) throws Exception{
		if(properties==null||propertyName==null||codeType==null){
			return null;
		}
		Map resultMap = new HashMap();
		Object object = properties.get(propertyName);
		if(object==null){
//			resultMap.put("isOk", "N");
			resultMap.put("msg", propertyName+"不在代码集范围");
			log.error(propertyName+"不在代码集范围");
			return resultMap;
		}
		SysStaticData sysStaticData = SysStaticDataUtil.getSysStaticData(codeType, String.valueOf(object));
		if(sysStaticData==null){
//			resultMap.put("isOk", "N");
			resultMap.put("msg", propertyName+"不在代码集范围");
			log.error(propertyName+"不在代码集范围");
			return resultMap;
		}
		return null;
	}
}
